#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void menu();//主菜单函数
void use();//各班学生用书表
void sale();//售书登记表
void lack();//缺书登记表
void stock();//教材存量表
void purch();//待购教材表
void less();//缺书单
void view();//教材一览表
void again();//返回主菜单 

struct booklist{
	char Class[20];     //班级 
	int ID;             //学号 
	char name[20];      //姓名 
	char ISBN[20];      //书号 
	char press[50];     //出版社 
	double price;       //单价 
	char add[100];      //地址 	
	char title[80];     //书名 
	int number;         //数量 
	char phone[30];     //电话 
};  

int main()
{
	menu();
	return 0;
}

void menu()//主菜单函数
{
	int x;
	printf("\n\n");
	printf("\t\t*****************************************\t\n");
	printf("\t\t*\t欢迎使用大学教材购销系统！    \t*\t\n");
    printf("\t\t*****************************************\t\n");
    printf("\t\t*\t请选择以下功能                \t*\t\n");
    printf("\t\t*****************************************\t\n");
	printf("\t\t*\t1.各班学生用书表              \t*\t\n");
	printf("\t\t*\t2.售书登记表                  \t*\t\n");
	printf("\t\t*\t3.缺书登记表                  \t*\t\n");
	printf("\t\t*\t4.教材存量表                  \t*\t\n");
	printf("\t\t*\t5.待购教材表                  \t*\t\n");
	printf("\t\t*\t6.缺书单                      \t*\t\n");
	printf("\t\t*\t7.教材一览表                  \t*\t\n");
	printf("\t\t*\t8.退出系统                    \t*\t\n");
	printf("\t\t*****************************************\t\n\n\n");
	printf("\t请选择：");	
	scanf("%d",&x);
	switch(x)
	{
		case 1 : 
		            system("cls");
			        use();//各班学生用书表
			        break;
		case 2 :
			        system("cls"); 
		            sale();//售书登记表
					break; 
		case 3 :
			        system("cls");
		            lack();//缺书登记表
					break;
		case 4 :	
		            system("cls");		
					stock();//教材存量表
		        	break;
		case 5 :    
		    	    system("cls");
					purch();//待购教材表
					break;
		case 6 :	
				    system("cls");
					less();//缺书单
					break;
		case 7 :
					system("cls");
					view();//教材一览表
					break;
		case 8 :
					return;
		default :
                    system("cls");
		         	printf("\n\n\t\t没有此选项\n\n");
		         	system("pause");
		         	again();
					break;				       
	}
}

void use()//各班学生用书表
{
	int i = 0;
	booklist stu[200];
	FILE *fp;
	fp = fopen("usebook.txt","r");//打开“usebook.txt”(各班学生用书表) 
	if(fp == NULL)
	{
		printf("未成功打开文件");
		return;
	}
	while(!feof(fp))//写入文件 
	{ 
		fscanf(fp,"%s",&stu[i].Class);
		fscanf(fp,"%s",&stu[i].ISBN);
		fscanf(fp,"%s",&stu[i].title);
		fscanf(fp,"%d\n",&stu[i].number);
		i++;
	}
	fclose(fp);
	int n = i;
		printf("\t*************************************************************************\t\n");
		printf("\t*\t班级\t\t书号\t\t\t书名\t\t数量\t*\t\n");
		printf("\t*************************************************************************\t\n");
	    for(i=0;i<n;i++)
	    {
		    printf("\t*************************************************************************\t\n");
		    printf("\t*\t%s",stu[i].Class);
		    printf("\t%s",stu[i].ISBN);
		    printf("\t%s",stu[i].title);
		    printf("\t%d\t*\t\n",stu[i].number);
		    printf("\t*************************************************************************\t\n");
	    }
	system("pause");
	again();//返回主菜单    
}

void sale()//售书登记表
{
	booklist stu[200];
	int i = 0;
	FILE *fp;
	fp = fopen("salebook.txt","r");//打开“salebook.txt”(售书登记表) 
	if(fp == NULL)
	{
		printf("未成功打开文件");
		return;
	}
	while(!feof(fp))
	{
		fscanf(fp,"%d",&stu[i].ID);
		fscanf(fp,"%s",&stu[i].name);
		fscanf(fp,"%s",&stu[i].ISBN);
		fscanf(fp,"%d\n",&stu[i].number);
		i++;
	}
	fclose(fp);
	int n = i;
	printf("\n\n\n");
	printf("\t*****************************************************************\t\n");
	printf("\t*\t\t\t    售书登记表    \t\t\t*\t\n");
	printf("\t*****************************************************************\t\n");
	printf("\t*\t学号\t\t姓名\t\t书号\t\t数量\t*\t\n");
	for(i=0;i<n;i++)
	{
		printf("\t*****************************************************************\t\n");
		printf("\t*\t%d",stu[i].ID);
		printf("\t%s",stu[i].name);
		printf("\t%s",stu[i].ISBN);
		printf("\t%d\t*\n",stu[i].number);
		printf("\t*****************************************************************\t\n");
	}
	system("pause");
	system("cls");
	printf("\n\t\t是否要继续操作？\t\t");
	printf("\n\t\t1.登记\t\t");
	printf("\n\t\t2.删除\t\t"); 
	printf("\n\t\t3.退出\t\t");
	printf("\n\t请选择\t");
	int x;
	scanf("%d",&x);
	if(x == 1)//登记一条记录 
	{
		system("cls");
		printf("\n\t学号：");
		scanf("%d",&stu[i].ID);
		printf("\t姓名：");
		scanf("%s",&stu[i].name);
		getchar();
		printf("\t书号：");
		scanf("%s",&stu[i].ISBN);
		getchar(); 
		printf("\t数量：");
		scanf("%d",&stu[i].number);
        fp = fopen("salebook.txt","a");
		if(fp == NULL)
	    {
		    printf("未成功打开文件");
		    return; 
	    }
        fprintf(fp,"%d %s %s %d\n",stu[i].ID,stu[i].name,stu[i].ISBN,stu[i].number);
		fclose(fp);
		printf("\n\t信息录入成功!");
		system("pause");
	    again();
    }
	if(x == 2)//删除一条记录 
	{
		system("cls");
		int j = 0;
		int id , flag = 0;
		char isbn[20];
		printf("\n\t请输入想要删除的学生的学号：");
		scanf("%d",&id);
		printf("\n\t请输入想要删除的学生的书号：");
		scanf("%s",isbn);
		getchar();
		for(i=0;i<n;i++)
		{
			if(strcmp(isbn,stu[i].ISBN) == 0&&id == stu[i].ID)
			{
				flag = 1;
				for(j=i;j<n-1;j++)
				{
					stu[j] = stu[j+1];
				}
			}
			if(flag == 1)
			{
				break;
			}
		}
		if(flag == 0)
		{
			printf("\n\t没有该条记录"); 
			system("pause");
	        again();
		}
		if(flag == 1)
		{
			i = 0;
			fp = fopen("salebook.txt","w");
			if(fp == NULL)
	        {
		        printf("未成功打开文件");
		        return; 
	        }
	        for(i=0;i<n-1;i++)
	        {
		        fprintf(fp,"%d ",stu[i].ID);
		        fprintf(fp,"%s ",stu[i].name);
	        	fprintf(fp,"%s ",stu[i].ISBN);
		        fprintf(fp,"%d\n",stu[i].number);
		    }
	        fclose(fp);
			printf("\n\t删除成功");	
		}
		system("pause");
	    again();
	}
	if(x == 3)//返回主菜单 
	{
		again();
	}
}

void lack()//缺书登记表
{
	booklist stu[200];
	int i = 0;
	FILE *fp;
	fp = fopen("lackbook.txt","r"); //打开“lackbook.txt”(缺书登记表)。 
	if(fp == NULL)
	{
		printf("未成功打开文件");
		return;
	}
	while(!feof(fp))
	{
		fscanf(fp,"%d",&stu[i].ID);
		fscanf(fp,"%s",&stu[i].name);
		fscanf(fp,"%s",&stu[i].ISBN);
		fscanf(fp,"%d\n",&stu[i].number);
		i++;
	}
	fclose(fp);
	int n = i;
	printf("\n\n\n");
	printf("\t*****************************************************************\t\n");
	printf("\t*\t\t\t    缺书登记表    \t\t\t*\t\n");
	printf("\t*****************************************************************\t\n");
	printf("\t*\t学号\t\t姓名\t\t书号\t\t数量\t*\t\n");
	for(i=0;i<n;i++)
	{
		printf("\t*****************************************************************\t\n");
		printf("\t*\t%d",stu[i].ID);
		printf("\t%s",stu[i].name);
		printf("\t%s",stu[i].ISBN);
		printf("\t%d\t*\n",stu[i].number);
		printf("\t*****************************************************************\t\n");
	}
	system("pause");
	system("cls");
	printf("\n\t\t是否要继续操作？\t\t");
	printf("\n\t\t1.登记\t\t");
	printf("\n\t\t2.修改\t\t");
	printf("\n\t\t3.删除\t\t");
	printf("\n\t\t4.退出\t\t");
	printf("\n\t请选择\t");
	int x;
	scanf("%d",&x);
	if(x == 1)//登记一条记录                        
	{
		system("cls"); 
		printf("\n\t学号：");
		scanf("%d",&stu[i].ID);
		printf("\t姓名：");
		scanf("%s",&stu[i].name);
		getchar();
		printf("\t书号：");
		scanf("%s",&stu[i].ISBN);
		getchar(); 
		printf("\t数量：");
		scanf("%d",&stu[i].number);
        fp = fopen("lackbook.txt","a");
		if(fp == NULL)
	    {
		    printf("未成功打开文件");
			return; 
	    }
        fprintf(fp,"\n%d %s %s %d",stu[i].ID,stu[i].name,stu[i].ISBN,stu[i].number);
		fclose(fp);
		printf("\n\t信息录入成功!");
		system("pause");
	    again();
    }
	
	if(x == 2)//修改数量 
	{
	    system("cls");
		int id , number , flag = 0;
		char isbn[20];
		printf("\n\t请输入想要修改的学生的学号：");
		scanf("%d",&id);
		printf("\n\t请输入想要修改的学生的书号：");
		scanf("%s",isbn);
		getchar();
		printf("\n\t请输入新数量：");
		scanf("%d",&number);
		for(i=0;i<n;i++)
		{
			if(strcmp(isbn,stu[i].ISBN) == 0&&id == stu[i].ID)
			{
				flag = 1;
				stu[i].number = number;
			}
			if(flag == 1)
			{
				break;
			}
		}
		if(flag == 0)
		{
			printf("\n\t没有该条记录"); 
			system("pause");
			again();
		}
		if(flag == 1)
		{
			i = 0;
			fp = fopen("lackbook.txt","w");
			if(fp == NULL)
	        {
		        printf("未成功打开文件");
		        return; 
	        }
	        for(i=0;i<n;i++)
	        {
		        fprintf(fp,"%d ",stu[i].ID);
		        fprintf(fp,"%s ",stu[i].name);
	        	fprintf(fp,"%s ",stu[i].ISBN);
		        fprintf(fp,"%d\n",stu[i].number);
		    }
	        fclose(fp);
			printf("\n\t修改成功");	
		}
		system("pause");
	    again();	
    }
    if(x == 3)//删除一条记录 
	{
		system("cls");
		int j = 0;
		int id , flag = 0;
		char isbn[20];
		printf("\n\t请输入想要删除的学生的学号：");
		scanf("%d",&id);
		printf("\n\t请输入想要删除的学生的书号：");
		scanf("%s",isbn);
		getchar();
		for(i=0;i<n;i++)
		{
			if(strcmp(isbn,stu[i].ISBN) == 0&&id == stu[i].ID)
			{
				flag = 1;
				for(j=i;j<n-1;j++)
				{
					stu[j] = stu[j+1];
				}
			}
			if(flag == 1)
			{
				break;
			}
		}
		if(flag == 0)
		{
			printf("\n\t没有该条记录"); 
			system("pause");
	        again();
		}
		if(flag == 1)
		{
			i = 0;
			fp = fopen("lackbook.txt","w");
			if(fp == NULL)
	        {
		        printf("未成功打开文件");
		        return; 
	        }
	        for(i=0;i<n-1;i++)
	        {
		        fprintf(fp,"%d ",stu[i].ID);
		        fprintf(fp,"%s ",stu[i].name);
	        	fprintf(fp,"%s ",stu[i].ISBN);
		        fprintf(fp,"%d\n",stu[i].number);
		    }
	        fclose(fp);
			printf("\n\t删除成功");	
		}
		system("pause");
	    again(); 
	}
    if(x == 4)//返回主菜单 
    {
    	again(); 
	}
}

void stock()//教材存量表
{
	int i = 0;
	booklist book[200];
	FILE *fp;
	fp = fopen("stockbook.txt","r");
	if(fp == NULL)
	{
		printf("未成功打开文件");
		return;
	}
	while(!feof(fp))
	{
		fscanf(fp,"%s",&book[i].ISBN);
		fscanf(fp,"%lf",&book[i].price);
		fscanf(fp,"%d\n",&book[i].number);
		i++;
	}
	int n = i;
	printf("\t*********************************************************\t\n");
	printf("\t*\t\t\t教材存量表\t\t\t*\t\n");
	printf("\t*********************************************************\t\n");
	printf("\t*\t书号\t\t\t单价\t\t数量\t*\t\n");
	printf("\t*********************************************************\t\n");
	for(i=0;i<n;i++)
	{
		printf("\t*********************************************************\t\n");
		printf("\t*\t%s",book[i].ISBN);
		printf("\t%.2lf",book[i].price);
		printf("\t\t%d\t*\t\n",book[i].number);
		printf("\t*********************************************************\t\n");
	}
	system("pause");
	system("cls");
	int x;
	printf("\n\t是否修改存量？");
	printf("\n\t1.修改");
	printf("\n\t2.退出"); 
	printf("\n\t请选择："); 
	scanf("%d",&x);
	if(x == 1)
	{
		system("cls");
		int flag,number;
		char isbn[20];
		printf("\n\t请输入书号：");
		scanf("%s",isbn);
		getchar();
		printf("\n\t请输入新存量：");
		scanf("%d",&number);
		flag = 0;
		for(i=0;i<n;i++)
		{
			if(strcmp(isbn,book[i].ISBN) == 0)
			{
				flag = 1;
				book[i].number = number;
			}
			if(flag == 1)
			{
				break;
			}
		}
		if(flag == 0)
		{
			printf("\n\t未找到对应书号\n");
			system("pause");
	        again(); 
		}
		if(flag == 1)
		{
			i = 0;
			fp = fopen("stockbook.txt","w");
			if(fp == NULL)
	        {
		        printf("未成功打开文件");
		        return; 
	        }
	        for(i=0;i<n;i++)
	        {
	        	fprintf(fp,"%s ",book[i].ISBN);
	        	fprintf(fp,"%lf ",book[i].price);
		        fprintf(fp,"%d\n",book[i].number);
		    }
	        fclose(fp);
			printf("\n\t修改成功\n");
			system("pause");
	        again();
		}
	}
	if(x == 2)
	{
		again();
	}
}

void purch()//待购教材表
{
	int i = 0;
	booklist book[200];
	FILE *fp;
	fp = fopen("pruchbook.txt","r");//打开“pruchbook.txt”（代购教材表）。 
	if(fp == NULL)
	{
		printf("未成功打开文件");
		return;	 
	}
	while(!feof(fp))
	{
		fscanf(fp,"%s",&book[i].ISBN);
		fscanf(fp,"%d\n",&book[i].number);
		i++;
	}
	int n = i;
	printf("\t*********************************************************\t\n");
	printf("\t*\t\t\t待购教材表\t\t\t*\t\n");
	printf("\t*********************************************************\t\n");
	printf("\t*\t书号\t\t\t\t数量\t\t*\t\n");
	printf("\t*********************************************************\t\n");
	for(i=0;i<n;i++)
	{
		printf("\t*********************************************************\t\n");
		printf("\t*\t%s",book[i].ISBN);
		printf("\t\t%d\t\t*\t\n",book[i].number);
		printf("\t*********************************************************\t\n");
	}
	system("pause");
	system("cls");
	printf("\n\t\t是否要继续操作？\t\t");
	printf("\n\t\t1.登记\t\t");
	printf("\n\t\t2.修改\t\t");
	printf("\n\t\t3.删除\t\t");
	printf("\n\t\t4.退出\t\t"); 
	printf("\n\t请选择\t");
	int x;
	scanf("%d",&x);
	if(x == 1)
	{
		system("cls");
		printf("\n\t书号：");
		scanf("%s",&book[i].ISBN);
		getchar(); 
		printf("\n\t数量：");
		scanf("%d",&book[i].number);
		fp = fopen("pruchbook.txt","a");
	    if(fp == NULL)
	    {
		    printf("未成功打开文件");
			return; 
	    }
	    fprintf(fp,"%s %d\n",book[i].ISBN,book[i].number);
	    fclose(fp);
	    printf("\n\t信息录入成功!");
	    system("pause");
	    again();
	}
	if(x == 2)
	{
		system("cls");
		char isbn[20];
		int number , flag = 0;
		printf("\n\t书号：");
		scanf("%s",isbn);
		getchar(); 
		printf("\n\t新数量：");
		scanf("%d",&number);
		for(i=0;i<n;i++)
		{
			if(strcmp(isbn , book[i].ISBN) == 0)
			{
				flag = 1;
				book[i].number = number;
			}
			if(flag == 1)
			{
				break;
			}
		}
		if(flag == 0)
		{
			printf("\n\t未找到对应书号\n");
			system("pause");
	        again();
		}
		if(flag == 1)
		{
			fp = fopen("pruchbook.txt","w");
	        if(fp == NULL)
	        {
		        printf("未成功打开文件");
			    return; 
	        }
	        for(i=0;i<n;i++)
	        {
	        	fprintf(fp,"\n%s %d",book[i].ISBN,book[i].number);
			}
	        fclose(fp);
	        printf("\n\t信息修改成功!");
		}
		system("pause");
	    again();
	}
	if(x == 3)
	{
		system("cls");
		int flag = 0;
		char isbn[20];
		printf("\n\t请输入要删除的书号：");
		scanf("%s",isbn);
		getchar();
		for(i=0;i<n;i++)
		{
			if(strcmp(isbn,book[i].ISBN)==0)
			{
				flag = 1;
				for(int j=i;j<n-1;j++)
				{
					book[j] = book[j+1];
				}
			}
			if(flag == 1)
			{
				break;
			}
		}
		if(flag == 0)
		{
			printf("\n\t未找到对应书号");
			system("pause");
			again(); 
		}
		if(flag == 1)
		{
			fp = fopen("pruchbook.txt","w");
			if(fp == NULL)
	        {
		        printf("未成功打开文件");
		        return;	 
	        }
			for(i=0;i<n-1;i++)
			{
				fprintf(fp,"%s ",book[i].ISBN);
				fprintf(fp,"%d\n",book[i].number);
			}
			fclose(fp);
			printf("\n\t删除成功");
			system("pause");
			again(); 
		}
	}
	if(x == 4)
	{
		again();
	}
}

void less()//缺书单
{
	int i = 0;
	booklist book[200];
	FILE *fp;
	fp = fopen("lessbook.txt","r");//打开“lessbook.txt”（缺书单）。 
	if(fp == NULL)
	{
		printf("未成功打开文件");
		return;	 
	}
	while(!feof(fp))
	{
		fscanf(fp,"%s",&book[i].ISBN);
		fscanf(fp,"%d",&book[i].number);
		fscanf(fp,"%s",&book[i].press);
		fscanf(fp,"%s",&book[i].add);
		fscanf(fp,"%s",&book[i].phone);
		i++;
	}
	fclose(fp);
	printf("\t*********************************************************************************\t\n");
	printf("\t*\t\t\t\t缺书单  \t\t\t\t\t*\t\n");
	printf("\t*********************************************************************************\t\n");
	printf("\t*\t书号\t\t\t数量\t出版社\t\t地址\t电话\t\t*\t\n");
	int n = i;
	for(i=0;i<n;i++)
	{
		printf("\t*********************************************************************************\t\n");
		printf("\t*\t%s",book[i].ISBN);
		printf("\t%d",book[i].number);
		printf("\t%s",book[i].press);
		printf("\t%s",book[i].add);
		printf("\t%s\t*\n",book[i].phone);
		printf("\t*********************************************************************************\t\n");
	}
	system("pause");
	system("cls");
	printf("\n\t\t是否要继续操作？\t\t");
	printf("\n\t\t1.登记\t\t");
	printf("\n\t\t2.删除\t\t");
	printf("\n\t\t3.退出\t\t");
	printf("\n\t请选择\t");
	int x;
	scanf("%d",&x);
	if(x == 1)//登记一条记录 
	{
		system("cls");
		printf("\n\t书号：");
		scanf("%s",&book[i].ISBN);
		getchar();
		printf("\t数量：");
		scanf("%d",&book[i].number);
		printf("\t出版社：");
		scanf("%s",&book[i].press);
		getchar(); 
		printf("\t电话：");
		scanf("%s",&book[i].phone);
		getchar();
		fp = fopen("lessbook.txt","a");
		if(fp == NULL)
	    {
		    printf("未成功打开文件");
		    return;
	    }
		fprintf(fp,"\n%s %d %s %s %s",book[i].ISBN,book[i].number,book[i].press,book[i].phone);
		fclose(fp);
		printf("\n\t信息录入成功!");
		system("pause");
		again();
	}
	if(x == 2)//删除一条记录 
	{
		system("cls");
		int flag = 0;
		char isbn[20];
		printf("\n\t书号：");
		scanf("%s",isbn);
		getchar();
		for(i=0;i<n;i++)
		{
			if(strcmp(isbn,book[i].ISBN) == 0)
			{
				flag = 1;
				for(int j=i;j<n-1;j++)
				{
					book[j] = book[j+1];
				}
				if(flag == 1)
				{
					break;
				}
			}
		}
		if(flag == 0)
		{
			printf("\n\t未找到对应书号");
			system("pause");
			again();
		}
		if(flag == 1)
		{
			fp = fopen("lessbook.txt","w");
			if(fp == NULL)
	        {
		        printf("未成功打开文件");
		        return;	 
	        }
			for(i=0;i<n-1;i++)
			{
				fprintf(fp,"\n%s ",book[i].ISBN);
		        fprintf(fp,"%d ",book[i].number);
		        fprintf(fp,"%s ",book[i].press);
		        fprintf(fp,"%s ",book[i].add);
		        fprintf(fp,"%s",book[i].phone);
			}
			fclose(fp);
			printf("\n\t删除成功"); 
			system("pause");
			again(); 
		}
	}
	if(x == 3)//返回主菜单 
	{
		again();
	}
}

void view()//教材一览表
{
	int i = 0;
	booklist book[200];
	FILE *fp;
	fp = fopen("viewbook.txt","r");//打开“viewbook.txt”（教材一览表）。 
	if(fp == NULL)
	{
		printf("未成功打开文件");
		return;	 
	}
	while(!feof(fp))
	{
		fscanf(fp,"%s",&book[i].ISBN);
		fscanf(fp,"%s",&book[i].title);
		fscanf(fp,"%s",&book[i].press);
		fscanf(fp,"%s",&book[i].add);
		fscanf(fp,"%s",&book[i].phone);
		i++;
	}
	fclose(fp);
	printf("\t*********************************************************************************************************\n");
	printf("\t*\t\t\t\t\t\t教材一览表\t\t\t\t\t\t*\n");
	printf("\t*********************************************************************************************************\n");
	printf("\t*\t书号\t\t\t书名\t\t\t出版社\t\t地址\t电话\t\t\t*\n");
	int n = i;
	for(i=0;i<n;i++)
	{
		printf("\t*********************************************************************************************************\n");
		printf("\t*\t%s",book[i].ISBN);
		printf("\t%s",book[i].title);
		printf("\t%s",book[i].press);
		printf("\t%s",book[i].add);
		printf("\t%s\t*\n",book[i].phone);
		printf("\t*********************************************************************************************************\n");
	}
	system("pause");
	again();
}

void again()//返回主菜单
{
	system("cls");
	menu();
}